package com.array;

import java.util.Arrays;

/***
 * @ClassName: BubbleSort
 * @Description:
 * @Auther: 送一一块二向箔
 * @Date: 16:52 2022/1/25
 * @version : V1.0
 */
public class BubbleSort {

    public static void main(String[] args) {
        int[] ints = {32,34,45,3,6,89,2,6,1,324};//length = 10

        int[] test = new int[1000];

        Arrays.fill(test,10);
        test [999] = 6;

        System.out.println(Arrays.toString(ints));

        long start = System.currentTimeMillis();
        System.out.println(Arrays.toString(bubbleSort(test)));
        long end = System.currentTimeMillis();
        System.out.println(end-start);


    }


    //冒泡排序
    static int[] bubbleSort (int[] arr) {
        boolean flag = true;
        int temp;
        //因为后面使用了j+1 所以这里要少一个。只用循环比到倒数第二个就可以了
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-1-i; j++) {
                if(arr[j]<arr[j+1]){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                    flag = false;
                }
            }
            if (flag) {
                break;
            }
        }

        return arr;
    }
}
